package com.huaxin.hxmoduleworkorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.hxmoduleworkorder.dto.request.MaintenanceOrderElectricianTaskProcessCreateReqDTO;
import com.huaxin.hxmoduleworkorder.mapper.MaintenanceOrderElectricianTaskProcessMapper;
import com.huaxin.hxmoduleworkorder.mapping.MaintenanceOrderElectricianTaskProcessMapping;
import com.huaxin.hxmoduleworkorder.models.MaintenanceOrderElectricianTaskProcess;
import com.huaxin.hxmoduleworkorder.service.MaintenanceOrderElectricianTaskProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MaintenanceOrderElectricianTaskProcessServiceImpl extends ServiceImpl<MaintenanceOrderElectricianTaskProcessMapper, MaintenanceOrderElectricianTaskProcess> implements MaintenanceOrderElectricianTaskProcessService {
    private final ApplicationContext applicationContext;
    private final MaintenanceOrderElectricianTaskProcessMapping mapping;

    @Override
    public Boolean create(MaintenanceOrderElectricianTaskProcessCreateReqDTO in) {
        if (in == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getMaintenanceOrderElectricianTaskId())) {
            return false;
        }
        MaintenanceOrderElectricianTaskProcess entity = mapping.toEntity(in);
        return this.save(entity);
    }

    @Override
    public Boolean createProcesses(List<MaintenanceOrderElectricianTaskProcessCreateReqDTO> in) {
        if (in == null || in.isEmpty()) {
            return false;
        }
        List<MaintenanceOrderElectricianTaskProcess> entityList = mapping.toEntityList(in);
        return this.saveBatch(entityList);
    }

    @Override
    public List<MaintenanceOrderElectricianTaskProcess> getProcessHistory(String taskId) {
        LambdaQueryWrapper<MaintenanceOrderElectricianTaskProcess> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaintenanceOrderElectricianTaskProcess::getMaintenanceOrderElectricianTaskId, taskId)
                .orderByAsc(MaintenanceOrderElectricianTaskProcess::getCreatedAt);
        return this.baseMapper.selectList(wrapper);
    }
}